﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using System.Xml.XLinq;

namespace WpfApplicationExperimental
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window , IListView 
    {
        DocumentViewController documentController;

        public MainWindow()
        {
            InitializeComponent();

            documentController = new DocumentViewController(); documentController.listView = this;

            this.documentDetailedControl1.OnCreated += new Action(documentDetailedControl1_OnCreated);

            ObjectDataProvider employeeProvider = this.FindResource("Employees") as ObjectDataProvider;

            this.treeView1.ItemsSource = MainManusCategoryGenerator.GetMainMenuCategoryItems("Documents");
            this.failuresTreeView.ItemsSource = MainManusCategoryGenerator.GetMainMenuCategoryItems("failures");
            this.personeltreeview.ItemsSource = MainManusCategoryGenerator.GetMainMenuCategoryItems("Personel");
            this.projectsTreeView.ItemsSource = MainManusCategoryGenerator.GetMainMenuCategoryItems("projects");
            this.suppliesTreeView.ItemsSource = MainManusCategoryGenerator.GetMainMenuCategoryItems("projects");

            MainManusCategoryGenerator .Commands .Add ("Eγγραφα",new MainManusCategoryGenerator.MenuCommandItem ( "Eγγραφα", delegate (){this.ListDocuments ();}));
            MainManusCategoryGenerator.Commands.Add("Eργαζομενοι", new MainManusCategoryGenerator.MenuCommandItem("Eργαζομενοι", delegate() { this.ListEmployees (); }));
        }

        void documentDetailedControl1_OnCreated()
        {
            documentController.RequestNewDocumentCreation(documentDetailedControl1.SourceDocument as DataAccesLayer.Document);
        }

        private void TabItem_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void DataGrid_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
        {
   
        }

        private void CustomerGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            DataGrid grid = sender as DataGrid;
            DataAccesLayer.Document doc = grid.SelectedItem as DataAccesLayer.Document;

            ObjectDataProvider employeeProvider = this.FindResource("Employees") as ObjectDataProvider;
            if (doc != null)
            {
                employeeProvider.MethodParameters[0] = doc.Id.ToString();
                this.documentDetailedControl1.Visibility = Visibility.Visible;
                this.documentDetailedControl1.DispalyDocumentDetails(doc);
            }
        }

        private void TabItem_MouseDown_1(object sender, MouseButtonEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            documentController.SubmitChanges();
        }

        #region IListView Members

        public void UpdateList()
        {

        }

        #endregion

        private void TabItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
 
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

            System.Collections.IList docs = this.MainGridView.SelectedItems;

            foreach (DataAccesLayer.Document doc in docs)
                this.documentController.RequestDocumentDelete(doc);
        }

        private void Documentsgrid_InitializingNewItem(object sender, InitializingNewItemEventArgs e)
        {

        }

        private void Documentsgrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {

        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {

        }

        private void DeleteEmployee(object sender, RoutedEventArgs e)
        {
            documentController.RequestAccosiationRemoveToDocument(((DataAccesLayer.Document)MainGridView.SelectedItem).Id, ((DataAccesLayer.Employee)Employeegrid.SelectedItem).id);
        }

        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if ((this.treeView1.SelectedItem).ToString() == "Εγγραφα")
                MainManusCategoryGenerator.Commands["Eγγραφα"].Execute();
        }

        #region Listing Methodes 
        protected void ListDocuments()
        {
            System.Collections.IEnumerable documents = new DataAccesLayer.DocumentDataAccesLayer().GetDocuments();

            this.MainGridView.ItemsSource = documents;
        }

        private void ListEmployees()
        {
            System.Collections.IEnumerable employees = new DataAccesLayer.EmployeeDataAccesLayer().GetEmployees();

            this.MainGridView.ItemsSource = employees;
        } 
        #endregion

        private void personeltreeview_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if ((this.personeltreeview .SelectedItem).ToString() == "Εργαζομενοι")
                MainManusCategoryGenerator.Commands["Eργαζομενοι"].Execute();
        }

        #region IListView Members


        public void ErrorOnNewAddition(object document)
        {
            this.MainGridView.CancelEdit();
        }

        #endregion
    }



}
